﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class WeaponSlotIcon : MonoBehaviour
{
    public Image Perview;
    public GameObject CoolingMask;
    public GameObject UAVNIconPerfab;
    public GameObject UAVNIconListParentObj;
    List<GameObject> UAVNIconList = new List<GameObject>();
    int m_CurrentUAVNumber = 0;

    public WeaponSlot Slot
    {
        set {
            m_Slot = value;
            if(Perview != null && m_Slot != null && m_Slot.m_Weapon != null)
            {
                Perview.sprite = Resources.Load<Sprite>(m_Slot.m_Weapon.Perview);
                if(m_Slot.m_Weapon as UAV != null && UAVNIconList.Count == 0)
                {
                    for (int i = 0; i < 9; ++i)
                    {
                        var icon = Instantiate(UAVNIconPerfab);
                        UAVNIconList.Add(icon);
                        icon.transform.SetParent(UAVNIconListParentObj.transform);
                        icon.transform.localScale = new Vector3(1, 1, 1);
                        icon.SetActive(false);
                    }
                }
            }
        }
        get { return m_Slot; }
    }
    WeaponSlot m_Slot;
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (null == m_Slot || /*!m_Slot.gameObject.activeSelf ||*/ null == m_Slot.m_Weapon)
            return;
        float process = 1.0f - Mathf.Clamp((m_Slot.ShutCooling / m_Slot.m_Weapon.ShutVelocity)+(m_Slot.m_FireReady?1:0), 0f, 1f);
        var size = CoolingMask.GetComponent<RectTransform>().sizeDelta;
        size.y = GetComponent<RectTransform>().sizeDelta.y * process;
        CoolingMask.GetComponent<RectTransform>().sizeDelta = size;

        UAV uav = m_Slot.m_Weapon as UAV;
        if (uav != null)
        {
            if(m_CurrentUAVNumber != uav.CurrentNumberOfUAV)
            {
                m_CurrentUAVNumber = uav.CurrentNumberOfUAV;
                SetUAVNumberShow(m_CurrentUAVNumber);
            }
        }
    }

    void SetUAVNumberShow(int number)
    {
        for(int i=0; i< UAVNIconList.Count; ++i)
        {
            if(i<number)
            {
                UAVNIconList[i].SetActive(true);
            }
            else
                UAVNIconList[i].SetActive(false);

        }
    }
}
